<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
   <script type="text/javascript">

		//获取产品信息
		var store = new Ext.data.JsonStore({
			root : "data",//从返回的json对象data中读取数据
			totalProperty : "total",
			fields : ["prodId","prodName","prodTypeId","prodTypeName", 
			   		  "colorFlag","prodParId","prodFlag"],
			url : "product!GridList.action"
    	});

		//产品类型下拉列表store
		var type_store = new Ext.data.JsonStore( {
			root : "data",//从返回的json对象data中读取数据
			fields : [ "id", "text", "leaf"],
			url : "product!ProdType.action"
		});

		//卡类型下拉列表store
		var coflag_store= new Ext.data.JsonStore( {
			fields : ["id", "text"],
			data:[{id:"0",text:"带底板"},{id:"1",text:"不带底板"}]
		}); 

		//模板id数据源
		var prodid_store = new Ext.data.JsonStore({
			root : "data",
			fields : ["prodId" ],
			url : "product!doProdId.action"
		});
	
    	var reqtype="";
    	function request(rid,parid){    		
    		var ajaxurl="";
	    	var par={};
    		if("del"==reqtype){
    			ajaxurl="product!deleteProdInfo.action";
    			par={
    				"prodinty.prodId":rid,
    				"prodinty.prodParId":parid
    			};
    		}else{
	    		
	    		//如果为添加操作
	    		if("add"==reqtype){
	    			ajaxurl="product!addProdInfo.action";
	    		}//如果为修改操作
	    		else if("edit"==reqtype){
	    			ajaxurl="product!editProdInfo.action";
	    		}
	    		
				//获取产品信息
				var prodid = Ext.getCmp("prod_Id").getValue();
				var prodname = Ext.getCmp("prod_Name").getValue();
				var corflg = Ext.getCmp("color_Flag").getValue();
				var typename = Ext.getCmp("type_Name").getValue();
				if(!prodname || prodname==""){
    				//Ext.Msg.alert("提示","产品名称不能为空!");
    				Ext.Msg.show({
		              title:"警告",
		              msg: "产品名称不能为空!",
		              buttons: Ext.Msg.OK,
		              animEl: "elId",
		              icon:Ext.MessageBox.WARNING
		            });
    				return;
    			}    			
    			if(!Ext.getCmp("p2form").getForm().isValid ()){
					//Ext.Msg.alert("提示","请校验是否有特殊字符!");
					Ext.Msg.show({
		              title:"警告",
		              msg: "验证不通过!",
		              buttons: Ext.Msg.OK,
		              animEl: "elId",
		              icon:Ext.MessageBox.WARNING
		            });
    				return;
				}
				
				Ext.getCmp("save").setDisabled(true);
				//在卡类型中查找卡类型名称对应record索引
    			var index = type_store.find("text",typename,0,true,true);
    			if(index!=-1){
    				//若找到，则查其id
    				index = type_store.getAt(index).data.id;
    			} 
    			
				//展开所有树节点,避免选中不了
	    		Ext.getCmp("rolefuncpanel").expandAll();
	    		//获取城市树选择菜单项
	    		var t_funs=Ext.getCmp("rolefuncpanel").getChecked();
				var funs="";
				for(var i=0;i<t_funs.length;i++){
					funs+="|"+t_funs[i].id;
				}
				funs=funs.substring(1);
    			var parid;
    			var node = Ext.getCmp("prod_tree").getSelectionModel().getSelectedNode();
    			if(node && "add"==reqtype){    				
    				parid = node.id;
       			}
    			var record = Ext.getCmp("prod_grid").getSelectionModel().getSelected();//获取当前选中grid项
    			if(record && "edit"==reqtype){
    				parid = record.data.prodParId;
    			}
    			//构造传递参数
    			par={
    				"prodinty.prodId":prodid,
					"prodinty.prodName":prodname,
					"prodinty.prodParId":parid,
					"prodinty.colorFlag":corflg,
					"prodinty.prodTypeId":index,
					idlist:funs	
					};		    		
    		}
			
			Ext.Ajax.request({
				clientValidation: true,
				params:par,
				url:ajaxurl,
				success: function(response){
					var d=eval("("+response.responseText+")");
					if(d.data){
						//Ext.Msg.alert("成功","操作成功!");
						Ext.Msg.show({
		                    title:"提示",
		                    msg:"操作成功!",
		                    buttons:Ext.Msg.OK,
		                    icon: Ext.MessageBox.INFO
		                });
						var tree = Ext.getCmp("prod_tree");
						var node = tree.getSelectionModel().getSelectedNode();//获取菜单树当前选择项
						//var record = Ext.getCmp("prod_grid").getSelectionModel().getSelected();//获取当前选中grid项
						//刷新树条件
						if("add"==reqtype){
							if(node){			
								if(node.isLeaf()){
									node.parentNode.reload();
								}else{
									node.reload();
								}
							}
						}else{
							//修改删除操作的是最子节点，因此刷新其父节点
							var nm = tree.getNodeById(parid);
							//判断是否有子节点,父节是否为跟节点,回调刷新树 
							if(nm){
							nm.reload(function(){
								if(!nm.hasChildNodes()&&"root"!=nm.parentNode.id){
									nm.parentNode.reload();								
								}
							},this);
							}				
						}					
						//刷新grid条件
						if("del"!=reqtype){
							store.baseParams["prodinty.prodId"] = prodid;
							delete store.baseParams["prodinty.prodParId"];	
						}else{							
							delete store.baseParams["prodinty.prodId"];
							store.baseParams["prodinty.prodParId"] = parid;	
						}
						Ext.getCmp("toolview").doRefresh();													
						Ext.getCmp("prod_panel").layout.setActiveItem(0);//显示第1界面
					} else {
						//Ext.Msg.alert("失败","操作失败!");
						Ext.Msg.show({
		                      title:"提示",
		                      msg:"操作失败!",
		                      buttons:Ext.Msg.OK,
		                      icon: Ext.MessageBox.ERROR
		                     });
					}
					Ext.getCmp("save").setDisabled(true);
				}
			});
    	}
    	
		var p1 = {
			id: "prod_grid",
			xtype: "grid",
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"prodName",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			store: store,
			headersDisabled: true,
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width: 30}),
	        	{header : "卡产品编号",dataIndex : "prodId",menuDisabled: true}, 
	    		{header : "卡产品名称",dataIndex : "prodName",id:"prodName",menuDisabled: true}, 
	    		{header : "彩打标识",dataIndex : "colorFlag",menuDisabled: true,
					renderer:function(input){
			             if(""==input)return input;
            			 var res2=coflag_store.getById(input);
						 return res2.data.text;
            		}
				},
				{header : "卡分类名称",dataIndex : "prodParId",menuDisabled: true,
					renderer:function(input){
						var node = Ext.getCmp("prod_tree").getNodeById(input);
						if(node){
							return node.text;
						}
					}
				}, 
				{header : "卡类别名称",dataIndex : "prodTypeName",menuDisabled: true}
			],
        	buttonAlign:"center",
        	bbar: new Ext.ux.PagingToolbar({
        		id:"toolview",
            	pageSize: limit,
            	store:store
        	}),
			buttons:[{text:"新增", handler: function(){
				reqtype="add";
				//获取产品树中的选择节点
				var node=Ext.getCmp("prod_tree").getSelectionModel().getSelectedNode();
				//如果有选择节点
				if(node){
						//城市树异步传参加载
				   	 	var tree=Ext.getCmp("rolefuncpanel");
				   		tree.getLoader().baseParams.cardid=node.id;
				   		//重新加载树
					   	tree.getRootNode().reload();
					  	//调用方法
						Ext.getCmp("p2form").resetParam();
						//激活显示第二页
						Ext.getCmp("prod_panel").layout.setActiveItem(1);
				}else{
					//Ext.Msg.alert("错误","未选中任何节点,无法进行此操作!");
					Ext.Msg.show({
		              title:"警告",
		              msg: "未选中任何节点,无法进行此操作!",
		              buttons: Ext.Msg.OK,
		              animEl: "elId",
		              icon:Ext.MessageBox.WARNING
		              });
				}
			}},{text:"修改", handler: function(){
				reqtype="edit";
				var record=Ext.getCmp("prod_grid").getSelectionModel().getSelected();//获取列表中被选择的数据
				if (record){
					Ext.getCmp("p2form").setParam(record.data);
					//城市树异步传参加载
			   	 	var tree=Ext.getCmp("rolefuncpanel");
			   		tree.getLoader().baseParams.cardid=record.data.prodId;//给后台carid传参
			   		//重新加载树
			   		tree.getRootNode().reload();
					Ext.getCmp("prod_panel").layout.setActiveItem(1);//激活显示第二页
				}else{
					//Ext.Msg.alert("错误","未选中任何行信息,无法进行此操作!");
					Ext.Msg.show({
		              title:"警告",
		              msg: "未选中任何行信息,无法进行此操作!",
		              buttons: Ext.Msg.OK,
		              animEl: "elId",
		              icon:Ext.MessageBox.WARNING
		              });
				}
			}},{text:"删除",handler: function(){
				var p1 = this.ownerCt.ownerCt;
				var record = Ext.getCmp("prod_grid").getSelectionModel().getSelected();
				if (record){
					var node = Ext.getCmp("prod_tree").getNodeById(record.data.prodId);
					if(!node.isLeaf()){
						Ext.Msg.show({
                            title:"错误",
                            msg: "该卡产品目录下有启用产品,不能删除!",
                            buttons: Ext.Msg.OK,
                            animEl: "elId",
                            icon:Ext.MessageBox.ERROR
                   	 	});
						return;
					}
					Ext.Msg.confirm("警告","确定要删除吗？",function(btn){
						if(btn == "yes"){
							reqtype="del";
							request(record.data.prodId,record.data.prodParId);
						}
					});
				}else{
					//Ext.Msg.alert("错误","未选中任何行信息,无法进行此操作!");
					Ext.Msg.show({
		              title:"警告",
		              msg: "未选中任何行信息,无法进行此操作!",
		              buttons: Ext.Msg.OK,
		              animEl: "elId",
		              icon:Ext.MessageBox.WARNING
		              });
				}
			}}]
		};

		//--------------------------------------------------------------------
		var p3 = {
			id:"p2form",
			title:"卡产品信息",
		    labelWidth: 70,
		    border:false,
		    region: "north",
		    height:160,
			xtype: "form",
		    bodyStyle:"padding:2px 5px 0",
			defaultType: "textfield",
			items: [{
				id : "prod_Id",
				fieldLabel : "卡产品编号",
				disabled : true,
				anchor : "100%"
			}, {
				id : "prod_Name",
				fieldLabel : "卡产品名称",
				allowBlank: false,
				vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项！";
		        		if(Ext.fucCheckCHLength(val)>50){
		        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	},
				name : "prodName",
				anchor : "100%"
			}, {
				id: "color_Flag",
				fieldLabel: "彩打标识",
				allowBlank: false,
				anchor:"100%",
				xtype:"billCombox",
				store: coflag_store
			}, {
				id : "prodPar_Id",
				fieldLabel : "卡分类名称",
				disabled : true,
				anchor : "100%"
			}, {
				id: "type_Name",
				fieldLabel: "卡类别名称",
				anchor:"100%",
				store:type_store,
				disabled : true,
				xtype:"billCombox"
			}],
			setParam: function(data){
				Ext.getCmp("p2form").showData=data;
				Ext.getCmp("prod_Id").setValue(data.prodId);//卡产品编号
				Ext.getCmp("prod_Name").setValue(data.prodName);//卡产品名称
				Ext.getCmp("color_Flag").setValue(data.colorFlag);//彩打标识
				Ext.getCmp("prodPar_Id").setValue(Ext.getCmp("prod_tree").getNodeById(data.prodParId).text);//卡分类名称
				Ext.getCmp("type_Name").setValue(data.prodTypeName);//卡类别名称
				},
			resetParam: function(){
				var node=Ext.getCmp("prod_tree").getSelectionModel().getSelectedNode();
				var typename;
				//根据当前选中树节点查找其卡类型
				if(node){
					var path = node.getPath();
					var id = path.split("/");					
					typename = type_store.getById(id[2]).data.text;
				}
				prodid_store.load({callback : function() {
					Ext.getCmp("prod_Id").setValue(this.getAt(0).data.prodId);//卡产品id
				}});
				Ext.getCmp("prod_Name").setValue("");//卡产品名称
				Ext.getCmp("color_Flag").setValue("0");//彩打标识
				Ext.getCmp("prodPar_Id").setValue(node.text);//卡分类名称
				Ext.getCmp("type_Name").setValue(typename);//卡类别名称
			}
			};
		//--------------------------------------------------------------------
		var p2 = {
			showData:null,
			layout:"border",
			items:[p3,
				{xtype:"billTreePanel",
			    id:"rolefuncpanel",
			    region: "center",
			    width:"100%",
				title:"城市卡类别设置",
				loader:{url:"treeCheck!cardCity.action",
						baseParams:{"cardid":"root"}
				},
				root:{
					id:"root"
				}
			}
			],
			buttonAlign:"center",
			buttons:[{text:"保存",id:"save",handler: function(e){

					request();
				}},{text:"取消", handler: function(){
					Ext.getCmp("prod_panel").layout.setActiveItem(0);//显示第一界面
				}}]
		};
		
		var prod_pel = new Ext.Panel({
			border : false, 
			layout: "border",
			items: [{
					id:"prod_tree",
					xtype: "billTreePanel",
					dataUrl: "tree!product.action",
					root: new Ext.tree.AsyncTreeNode({
						id:"root",//跟节点ID
						expanded : true//是否展开
					}),
					listeners: {
						"click": function(node){
							store.baseParams = {};
							if(node.isLeaf()){
								store.baseParams["prodinty.prodId"] = node.id;
								delete store.baseParams["prodinty.prodParId"];
							}else{
								delete store.baseParams["prodinty.prodId"];
								store.baseParams["prodinty.prodParId"] = node.id;								
							}
							store.load({params:{start : 0,limit : limit}});
							Ext.getCmp("prod_panel").layout.setActiveItem(0);//显示第1界面
						}
					}
				}, {
					id: "prod_panel",
					region: "center",
					border : false, 
					layout:"card",
					activeItem:0,//显示第一个容器
					items: [p1, p2]
			}]
		});	
		type_store.load();//类型下拉列表	
	
	
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(prod_pel);
	CenterPanel.doLayout();
	</script>
